﻿// register your Gadget's namespace
registerNamespace("Microsoft.Live.GadgetSDK");

// define the constructor for your Gadget (this must match the name in the manifest xml)
Microsoft.Live.GadgetSDK.NotepadGadget = function(p_elSource, p_args, p_namespace)
{
    // always call initializeBase before anything else!
	Microsoft.Live.GadgetSDK.NotepadGadget.initializeBase(this, arguments);

    // setup private member variables
	var m_this = this;
	var m_el = p_elSource;          // member variable to store the DOM element that contains the body of this Gadget
	var m_module = p_args.module;   // member variable to store the module of the Gadget
	var m_elInput = null;           // member variable to store the text area element object


	/****************************************
	**          initialize Method
	****************************************/
    // initialize is always called immediately after your object is instantiated
	this.initialize = function(p_objScope)
	{
	    // always call the base object's initialize first!	
		Microsoft.Live.GadgetSDK.NotepadGadget.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);

        // Create the text area DOM element
        m_elInput = document.createElement("textarea");
        m_el.appendChild(m_elInput);
        
        // Set the class name used in the css
        m_elInput.className = "TextArea";

        // Load the preferences (i.e. user data)
        LoadUserText();
        
        // Hook up event handlers
//        doAttachEvent(m_elInput, "keyup", adjustHeight);
//        doAttachEvent(m_elInput, "focus", textAreaOnFocus);
        doAttachEvent(m_elInput, "blur", textAreaOnBlur);
        	
	};
	Microsoft.Live.GadgetSDK.NotepadGadget.registerBaseMethod(this, "initialize");
	
	/****************************************	
	**           dispose Method
	****************************************/
	this.dispose = function(p_blnUnload)
	{    
	    // null out all member variables
	    m_this = null;
	    m_el = null;
	    m_module = null;
	    m_elInput = null;
        	    
	    // Detach events
	    doDetachEvent(m_elInput, "keyup", adjustHeight);
	    doDetachEvent(m_elInput, "focus", textAreaOnFocus);
	    doDetachEvent(m_elInput, "blur", textAreaOnBlur);
	    
        // always call the base object's dispose last!	
		Microsoft.Live.GadgetSDK.NotepadGadget.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);
	};
	Microsoft.Live.GadgetSDK.NotepadGadget.registerBaseMethod(this, "dispose");
	

	/****************************************
	** Private Methods
	****************************************/
	function textAreaOnFocus () 
	{
	    m_elInput.className = "TextAreaEditMode";
	}
	
	function textAreaOnBlur ()
	{
	    m_elInput.className = "TextArea";
	    SaveUserText();
	}
	
	function adjustHeight()
	{
    
	    m_elInput.style.height = m_elInput.scrollHeight + "px";
	    m_module.Resize();
	}
	
	function SaveUserText()
	{
	    // Check first to see if the Gadget is running on the dashboard (i.e. on the page).
	    // If it is in preview mode, we shouldn't save the data.
	    if (p_args.onDashboard)
	    {
	        var text = m_elInput.value;
	        text = escapeText(text);	        // we need to manually escape "\r\n" because of a platform bug
	        m_module.setPreference("TextValue", text);
	        m_module.setPreference("Height", m_elInput.style.height);

        }
	}
	
	function escapeText(txt)
	{
	    
	    
	    var sb = new Web.StringBuilder();
	    var i = 0;
	    
	    for ( i = 0; i < txt.length; i++ ) {
	        if ( txt.charAt(i) == "\r" ) {
	            sb.append("__r");
	        } else if (txt.charAt(i) == "\n") {
                sb.append("__n");
            } else {
	            sb.append(txt.charAt(i));
	        }
	        
	    }
	    return sb.toString();
	}
	
	function unescapeText(txt)
	{
	    var sb = new Web.StringBuilder();
	    var i = 0;
	    for ( i = 0; i < txt.length - 2; i++ ) {
	        if (txt.charAt(i) == "_" && txt.charAt(i+1) == "_" && txt.charAt(i+2) == "r") {
	            sb.append("\r");
	            i = i + 2;
	        } else if ( txt.charAt(i) == "_" && txt.charAt(i+1) == "_" && txt.charAt(i+2) == "n" ) {
	            sb.append("\n");
	            i = i + 2;
	        } else {
	            sb.append(txt.charAt(i));
	        }
	    }
	    sb.append(txt.charAt(i++));
	    sb.append(txt.charAt(i));
	    return sb.toString();    
    }
	
	function LoadUserText()
	{

	    // Check first to see if the Gadget is running on the dashboard (i.e. on the page).
	    // If it is in preview mode, we shouldn't load the data.
	    if (p_args.onDashboard)
	    {
	        var text = m_module.getPreference("TextValue");
	        if (text) {
	            text = unescapeText(text);      // we need to manually unescape "\r\n" because of a platform bug
	        }
	        var height = m_module.getPreference("Height");
	        
	        if (text)
	        {
	            m_elInput.value = text;
	        }
	        else
	        {
	            m_elInput.value = "";
	        }
	        
	        if (height)
	        {
	            m_elInput.style.height = height;
	        }
	        else
	        {
	            m_elInput.style.height = "60px";
	        }
	    }
	}
	
	function doAttachEvent(el, evtObj, Fn)
	{
		el.attachEvent("on" + evtObj, Fn);     // Atlas should handle the cross-browser compatibility
	}

	function doDetachEvent(el, evtObj, Fn) 
	{
		el.detachEvent("on" + evtObj, Fn);     // Atlas should handle the cross-browser compatibility
    }	

};
Microsoft.Live.GadgetSDK.NotepadGadget.registerClass("Microsoft.Live.GadgetSDK.NotepadGadget", "Web.Bindings.Base");